<template>
  <div>
    <Add ref="add" @ok="search"></Add>
    <Update ref="update" @ok="search"></Update>
    <ZgNewPage
      ref="page"
      :title="pageTitle"
      :showInlineBtn="false"
      :uniqueName="uniqueName"
      :pageOpt="pageOpt"
      :dataBtns="dataBtns"
      :extraBtns="extraBtns"
      :headers="headers"
      :conditions="conditions"
      :adminWidth="350"
      :scrollX="1500"
      @search="onSearch"
      @filter="onFilter"
      @header="mergeHeader"
      @admin="toAdmin"
      @extra="toExtra"
    >
      <template slot="columns">
        <a-table-column
          v-for="(h,i) in headers"
          :key="`column${i}`"
          :width="h.width===0 ? 'auto' : h.width"
          align="center"
          :title="h.label">
          <template slot-scope="record">
            <template v-if="h.code === 'F'">
              <ZgSwitch v-model="record[h.code]"></ZgSwitch>
            </template>
            <template v-else>{{ record[h.code] }}</template>
          </template>
        </a-table-column>
      </template>
      <template slot="thMode" slot-scope="scope">
        <a-row :gutter="10">
          <a-col :span="4" v-for="t in scope.list" :key="`thMode_${t.id}`">
            <a-card class="mb10">
              <div class="wfull h100 scroll">
                {{ t }}
              </div>
            </a-card>
          </a-col>
        </a-row>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
export default {
  components: {
    Add, Update
  },
  data () {
    return {
      pageTitle: '产品配置',
      uniqueName: 'test',
      pageOpt: {
        showConditionBtn: true,
        showHeaderConfig: true,
        showAttachBtn: true,
        showPageTitle: false
      },
      conditions: [
        { code: 'username', compType: '单行文本', label: '用户名', defaultVal: '' },
        { code: 'phone', compType: '单行文本', label: '手机号', defaultVal: '' },
        { code: 'companyName', compType: '下拉选择', label: '企业类型', defaultVal: '', extra: { 下拉选项: [] } },
      ],
      headers: [
        { code: 'username', label: '用户名', isShow: true, sort: 0 },
        { code: 'company', label: '所属企业', isShow: true, sort: 0 },
        // { code: 'C', label: '企业类型', isShow: true, sort: 0 },
        // { code: 'D', label: '关联角色', isShow: true, sort: 0 },
        { code: 'phone', label: '联系电话', isShow: true, sort: 0 },
        // { code: 'F', label: '用户状态', isShow: true, sort: 0 }
      ],
      dataBtns: [
        { key: 1, name: '详情', type: 'link', isShow: () => { return true } },
        { key: 2, name: '编辑', type: 'link', isShow: () => { return true } },
        { key: 3, name: '删除', type: 'link', isShow: () => { return true } },
        { key: 4, name: '初始化密码', type: 'link', isShow: () => { return true } }
      ],
      extraBtns: [
        { key: 1, name: '新增', icon: 'plus-circle', type: 'primary' }
      ],
      companyTypeList: []
    }
  },
  created () {
    // 获取企业类型
    this.getCompanyTypeList()
  },
  methods: {
    // 获取企业类型
    getCompanyTypeList() {
      const data = [
        { value: '1', label: '测试1' },
        { value: '2', label: '测试2' },
        { value: '3', label: '测试3' }
      ]
      setTimeout(() => {
        this.conditions[2].extra.下拉选项 = data
      }, 1000)

      this.$http.post('/api/sys/company/type/list', {}).then(res => {
        console.log('res', res)
        // if (res.code === 1) {
        //   this.companyTypeList = res.data
        // }
      })
    },
    search () {
      this.$refs.page.search()
    },
    onSearch (param) {
      const diyHeaders = localStorage.getItem(this.currentUser.username + '_diyHeader_' + this.uniqueName)
      if (diyHeaders) {
        this.mergeHeader(JSON.parse(diyHeaders))
      }

      this.getList(param)
    },
    getList(param) {
      this.$http.post('/zg-scf/user/page', param).then(res => {
        if (res.code === 1) {
          console.log('res.data.records', res.data.records)
          this.$refs.page.endSearch({
            total: res.data.records.length,
            list: res.data.records
          })
        }
      })
    },
    onFilter (f, param) {
    },
    toAdmin (param) {
      if (param.key === 1) {
       this.$refs.update.open(param.obj, 'details')
      } else if (param.key === 2) {
        this.$refs.update.open(param.obj, 'edit')
      } else if (param.key === 3) {
        this.confirm(
          () => {
            // this.$http.post('/zg-system/attach/del', [param.obj.id], () => {
            //   this.$refs.page.search()
            // })
          },
        '确定要删除这条数据么')
      } else if (param.key === 4) {
        alert('初始化密码')
      }
    },
    toExtra (opt) {
      this.$refs.add.open()
    },
    mergeHeader (newHeaders) {
      if (!newHeaders) {
        return
      }
      this.headers = newHeaders.filter(t => {
        return t.isShow
      }).sort((a, b) => {
        return a.sort - b.sort
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
